package com.vitabuybuy.tv.ui.mine.talent.task

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.Html
import androidx.core.os.bundleOf
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityPlantGrassTaskDetailBinding
import com.vitabuybuy.tv.dto.AddressListDto
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.dto.MessageEvent
import com.library.common.toast.XToast
import com.vitabuybuy.tv.ui.WebViewActivity
import com.vitabuybuy.tv.ui.mine.address.AddressActivity
import com.vitabuybuy.tv.ui.mine.talent.task.adapter.TaskGoodAdapter
import com.vitabuybuy.tv.ui.mine.talent.task.viewmodel.TaskDetailsViewModel
import com.vitabuybuy.tv.utils.EventCode
import kotlinx.android.synthetic.main.activity_plant_grass_task_detail.btnSignUp
import kotlinx.android.synthetic.main.activity_plant_grass_task_detail.ivChoose
import kotlinx.android.synthetic.main.activity_plant_grass_task_detail.lvGoods
import kotlinx.android.synthetic.main.activity_plant_grass_task_detail.rlreceiverAddress
import kotlinx.android.synthetic.main.activity_plant_grass_task_detail.tvAddressName
import kotlinx.android.synthetic.main.activity_plant_grass_task_detail.tvAgree
import kotlinx.android.synthetic.main.activity_plant_grass_task_detail.tvAreaRequirement
import kotlinx.android.synthetic.main.activity_plant_grass_task_detail.tvFansRequirement
import kotlinx.android.synthetic.main.activity_plant_grass_task_detail.tvName
import kotlinx.android.synthetic.main.activity_plant_grass_task_detail.tvReceiverAddressDetail
import kotlinx.android.synthetic.main.activity_plant_grass_task_detail.tvReceiverPhone
import kotlinx.android.synthetic.main.activity_plant_grass_task_detail.tvRequirement
import kotlinx.android.synthetic.main.activity_plant_grass_task_detail.tvRequirementContent
import org.greenrobot.eventbus.EventBus

class PlantGrassTaskDetailActivity :
    BaseActivity<TaskDetailsViewModel, ActivityPlantGrassTaskDetailBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_plant_grass_task_detail

    private var mTtaskId: Int? = -1
    private val goodAdapter by lazy { TaskGoodAdapter(false) }
    private var defaultAddId = -1


    override fun onGetBundle(bundle: Bundle?) {
        mTtaskId = bundle?.getInt("mTtaskId")
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.task_detail))
        mBinding.viewModel = viewModel
        tvAgree.text =
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                Html.fromHtml(
                    mContext.getString(R.string.read_and_agree),
                    Html.FROM_HTML_MODE_COMPACT
                )
            } else {
                Html.fromHtml(mContext.getString(R.string.read_and_agree))
            }
        lvGoods.apply {
            adapter = goodAdapter
            layoutManager = LinearLayoutManager(mContext)
            isNestedScrollingEnabled = false
        }
    }

    override fun observeData() {
        viewModel.getTaskPoolDetails(mTtaskId)
        viewModel.taskDetails.observe(this, Observer {
            tvName.text = it.store.name
            tvRequirement.text = it.title
            tvRequirementContent.text = String.format(
                getString(R.string.task_requirement), it.content
            )
            tvAreaRequirement.text =
                String.format(getString(R.string.area_requirement), it.label.name)
            tvFansRequirement.text =
                String.format(getString(R.string.fans_requirement), it.fansQuantity)
            goodAdapter.setNewData(it.productList)
            if (it.address != null) {
                tvAddressName.text = it.address.name
                tvReceiverPhone.text = it.address.phone
                tvReceiverAddressDetail.text = it.address.detail
                defaultAddId = it.address.id
            }
        })
    }

    override fun eventHandling() {
        rlreceiverAddress.setOnClickListener {
            AddressActivity.openActivity(mContext, 10002)
        }
        btnSignUp.setOnClickListener {
            if (defaultAddId == -1) {
                XToast.normal(getString(R.string.please_select_delivery_address))
                return@setOnClickListener
            }
            if (!ivChoose.isChecked) {
                XToast.normal(getString(R.string.pls_read_and_agree_agreement_2))
                return@setOnClickListener
            }
            val params = HashMap<String, Any?>()
            params["id"] = viewModel.taskDetails.value?.id
            params["address"] = mapOf(Pair("id", defaultAddId))
            viewModel.getTaskSign(params)
        }
        tvAgree.setOnClickListener {
            WebViewActivity.openActivity(mContext, "", "", 16)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) return
        if (requestCode == 10002) {
            val addressDto = data?.extras?.getSerializable("addressDto") as AddressListDto
            defaultAddId = addressDto.id
            tvAddressName.text = addressDto.name
            tvReceiverPhone.text = addressDto.phone
            tvReceiverAddressDetail.text = addressDto.detail
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            0 -> {
                viewModel.getTaskPoolDetails(mTtaskId)
            }
            1 -> {
                EventBus.getDefault().post(MessageEvent(EventCode.refush_task_list))
                finish()
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>, mTtaskId: Int) {
            mContext.startActivity(
                bundleOf(Pair("mTtaskId", mTtaskId)),
                PlantGrassTaskDetailActivity::class.java
            )
        }
    }
}
